{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 高斯朴素贝叶斯算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "概率: [0.5 0.5]\n",
      "样本数量: [3. 3.]\n",
      "标签 [0 1]\n",
      "均值 [[5. 4.]\n",
      " [4. 5.]]\n",
      "方差 [[ 2.66666667 14.00000001]\n",
      " [ 4.66666667  0.66666667]]\n",
      "预测结果: [0]\n",
      "预测结果的概率 [[0.87684687 0.12315313]]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "# 高斯朴素贝叶斯算法\n",
    "\n",
    "np.random.seed(0)\n",
    "\n",
    "x=np.random.randint(0,10,size=(6,2))\n",
    "y=np.array([0,0,0,1,1,1])\n",
    "data=pd.DataFrame(np.concatenate([x,y.reshape(-1,1)],axis=1),columns=['x1','x2','y'])\n",
    "\n",
    "gnb=GaussianNB()\n",
    "gnb.fit(x,y)\n",
    "# 每个类别的先验概率\n",
    "print('概率:',gnb.class_prior_)\n",
    "# 每个类别样本的数量\n",
    "print('样本数量:',gnb.class_count_)\n",
    "# 每个类别标签\n",
    "print('标签',gnb.classes_)\n",
    "# 每个特征在类别下的均值\n",
    "print('均值',gnb.theta_)\n",
    "# 每个特征在类别下的方差\n",
    "print('方差',gnb.sigma_)\n",
    "\n",
    "# 测试集\n",
    "x_test=np.array([[6,3]])\n",
    "print('预测结果:',gnb.predict(x_test))\n",
    "print('预测结果的概率',gnb.predict_proba(x_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 伯努利朴素贝叶斯算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数值1出现的次数 [[1. 1.]\n",
      " [1. 1.]]\n",
      "类别占比p(y): [0.5 0.5]\n",
      "特征概率: [[0.4 0.4]\n",
      " [0.4 0.4]]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>-5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2</td>\n",
       "      <td>-2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-3</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   x1  x2  y\n",
       "0   0  -5  0\n",
       "1  -2  -2  0\n",
       "2   2   4  0\n",
       "3  -2   0  1\n",
       "4  -3  -1  1\n",
       "5   2   1  1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.naive_bayes import BernoulliNB\n",
    "\n",
    "np.random.seed(0)\n",
    "x=np.random.randint(-5,5,size=(6,2))\n",
    "y=np.array([0,0,0,1,1,1])\n",
    "data=pd.DataFrame(np.concatenate([x,y.reshape(-1,1)],axis=1),columns=['x1','x2','y'])\n",
    "\n",
    "bnb=BernoulliNB()\n",
    "\n",
    "bnb.fit(x,y)\n",
    "# 每个特征在每个类别下发生(出现)的次数.因为伯努利分布只有两个值\n",
    "# 我们只需要计算出现的概率P(x=1\\y),不出现的概率P(x=0\\y)使用1减去P(x=1\\y)即可\n",
    "print('数值1出现的次数',bnb.feature_count_)  #类别0下x1出现1的次数,类别0下x2出现1的次数,一次类推\n",
    "# 每个类别样本占比,即P(y),该值为去对数之后的结果,如果需要查看原有概率,需要使用指数还原\n",
    "print('类别占比p(y):',np.exp(bnb.class_log_prior_))\n",
    "# 每个类别下,每个特征(值为1)所占的比例,即P(x\\y)\n",
    "print('特征概率:',np.exp(bnb.feature_log_prob_))\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多项式朴素贝叶斯算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3. 3.]\n",
      "[[4. 6.]\n",
      " [5. 8.]]\n",
      "[[0.41666667 0.58333333]\n",
      " [0.4        0.6       ]]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   x1  x2  y\n",
       "0   0   3  0\n",
       "1   1   0  0\n",
       "2   3   3  0\n",
       "3   3   3  1\n",
       "4   1   3  1\n",
       "5   1   2  1"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB\n",
    "\n",
    "np.random.seed(0)\n",
    "x=np.random.randint(0,4,size=(6,2))\n",
    "y=np.array([0,0,0,1,1,1])\n",
    "data=pd.DataFrame(np.concatenate([x,y.reshape(-1,1)],axis=1),columns=['x1','x2','y'])\n",
    "\n",
    "mnb=MultinomialNB()\n",
    "\n",
    "mnb.fit(x,y)\n",
    "# 每个类别的样本的数量\n",
    "print(mnb.class_count_)\n",
    "# 每个特征在每个类别下发生的次数\n",
    "print(mnb.feature_count_)  #x1在0的类别下出现的次数,x2在0的类别下出现的次数,以此类推\n",
    "# 每个类别下,每个特征所占的比例\n",
    "print(np.exp(mnb.feature_log_prob_))\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 利用鸢尾花数据集进行朴素贝叶斯分类,判定哪种效果最好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多项式朴素贝叶斯: 0.5789473684210527\n",
      "高斯朴素贝叶斯: 1.0\n",
      "伯努利朴素贝叶斯: 0.23684210526315788\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "x,y=load_iris(return_X_y=True)\n",
    "\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.25,random_state=0)\n",
    "\n",
    "models=[('多项式朴素贝叶斯:',MultinomialNB()),('高斯朴素贝叶斯:',GaussianNB()),('伯努利朴素贝叶斯:',BernoulliNB())]\n",
    "for name,m in models:\n",
    "    m.fit(x_train,y_train)\n",
    "    print(name,m.score(x_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
